package com.mj.mafeed.activity;

import java.util.Timer;
import java.util.TimerTask;

import com.mj.dollars.AppConnect;
import com.mj.mafeed.R;
import com.mj.mafeed.bean.ProductCategoryJSON;
import com.mj.mafeed.bean.ProductCategoryListJSON;
import com.mj.mafeed.config.Constants;
import com.mj.mafeed.service.ProductCategoryService;
import com.mj.mafeed.activity.ProductListActivity;
import com.mj.mafeed.util.KeyBoardUtils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TextView.OnEditorActionListener;

/**
 * 产品
 * @author zhaominglei
 * @date 2015-12-30
 * 
 */
public class ProductActivity extends Activity implements OnClickListener {
	@SuppressWarnings("unused")
	private static final String TAG = ProductActivity.class.getSimpleName();
	private ProductCategoryService productCategoryService = new ProductCategoryService();
	private boolean isExit = false;
	private TimerTask timerTask;
	
	private EditText keywordEdit; //关键字
	private String keyword;
	@SuppressWarnings("unused")
	private ImageView messageView; //信息
	private LinearLayout fenleiLayout; //商品分类
	private ImageLoader imageLoader = null;
	private Intent intent;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_product);
		init();
	}
	
	private void init() {
		keywordEdit = (EditText)findViewById(R.id.product_keyword_edit);
		messageView = (ImageView)findViewById(R.id.product_message);
		fenleiLayout = (LinearLayout)findViewById(R.id.product_fenlei_layout);
		
		fenleiLayout.setOnClickListener(this);
		keywordEdit.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
		keywordEdit.setOnEditorActionListener(new ProductEditorActionListener());
		imageLoader = ImageLoader.getInstance();
		imageLoader.init(ImageLoaderConfiguration.createDefault(getApplicationContext()));
		ProductCategoryListJSON productCategoryListJSON = productCategoryService.getAll(getApplicationContext());
		if (productCategoryListJSON != null && productCategoryListJSON.getRecordList() != null && !productCategoryListJSON.getRecordList().isEmpty()) {
			for(ProductCategoryJSON productCategoryJSON:productCategoryListJSON.getRecordList()) {
				ImageView fenleiView = new ImageView(getApplicationContext());
				fenleiLayout.addView(fenleiView);
				String url = Constants.SERVER_DOWNLOAD_URL + productCategoryJSON.getAppImage();
				imageLoader.displayImage(url, fenleiView);
			}
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.product_fenlei_layout:
			intent = new Intent(ProductActivity.this, ProductListActivity.class);
			intent.putExtra("referer", "ProductActivity");
			startActivity(intent);
			break;

		default:
			break;
		}
	}

	@Override
	public void onBackPressed() {
		if (isExit) {
			AppConnect.getInstance(this).close();
			ProductActivity.this.finish();
		} else {
			isExit = true;
			Toast.makeText(ProductActivity.this, R.string.exit_msg, Toast.LENGTH_SHORT).show();
			timerTask = new TimerTask() {
				@Override
				public void run() {
					isExit = false;
				}
			};
			new Timer().schedule(timerTask, 2*1000);
		}
	}
	
	public class ProductEditorActionListener implements OnEditorActionListener {
		@Override
		public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
			if (actionId == EditorInfo.IME_ACTION_SEARCH || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
				KeyBoardUtils.closeKeybord(keywordEdit, v.getContext());
				keyword = keywordEdit.getText().toString();
				Intent intent = new Intent(ProductActivity.this , ProductListActivity.class);
				intent.putExtra("referer", "ProductActivity");
				intent.putExtra("keyword", keyword);
				startActivity(intent);
				return true;
			}
			return false;
		}
	}	
}
